<?php
// +----------------------------------------------------------------------
// | 后台首页入口
// +----------------------------------------------------------------------
namespace app\admin\controller\soft;
use app\admin\Adminbase;
use app\admin\controller\soft\PageCreate;
use think\Controller;
use think\Request;

class Install extends Adminbase
{
    
    /**
    * 初始化系统 
    * admin/soft/Install/create 
    * 需要登录 
    * @is_need_login false
    */
    public function create(){
        
        if( file_exists( base_path().'/views/system/baseInstall/insall.lock' ) ){
           return $this -> ReError('不能二次初始化！');
        }
        
        $param = request()->post();
        
        try{
            //添加.env文件，配置数据库
            file_put_contents(base_path().'/.env',"
DB_CONNECTION=$param[type]
DB_HOST=$param[hostname]
DB_PORT=$param[hostport]
DB_DATABASE=$param[database]
DB_USERNAME=$param[username]
DB_PASSWORD=$param[password]
DB_PREFIX=$param[prefix]
        ");
            
            //初始化系统数据表
            $res = db_multi_query(file_get_contents(base_path().'system.sql'));
            file_put_contents(base_path().'/views/system/baseInstall/insall.lock','已安装初始数据');
            
            //添加管理员
            model('SystemUser')->addUser([
                "user_name"=>'管理员',        
                "user_code"=>$param['admin'],      
                "user_pwd"=>$param['admin_pwd'],
                "is_admin"=>1,
                "org_id"=>1
            ]);

            //创建页面
            (new PageCreate)->create();
            
            return $this -> ReSucess('',"操作成功！");
            
        } catch (\Exception $e) {
            unlink(base_path().'/views/system/baseInstall/insall.lock');
            return $this->ReError("链接错误：".$e->getMessage());    
        }
    }
	
    
    
    
    /** 
    * @func_code  
    * @action_code 
    * 后台入口
    * /admin/soft/install/xmlInstall
    */
    public function xmlInstall(Request $request){
        return false;
    }
    
	
}
